We're going to be adding a few files to Dudel, and
making changes to a few others, so now might be a good time to make a
copy of your Dudel project directory. That way, you can work with the
copy, and still have your previous version for reference in case
something goes wrong.
1. Preparing the Controller Interface
Let's start off, as
before, by dealing with the interface for our controller class. We're
going to have a new button for the Text tool, so DudelViewController
will get a new instance variable to point at that, as well as a new
action method for the button to call. Additionally, we'll add a new font
property, which the Text tool will access to figure out which font to
use to draw its text. The updated DudelViewController.h looks like this (the lines shown in bold text are the new parts):
// DudelViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import "Tool.h"
#import "DudelView.h"
@interface DudelViewController : UIViewController <ToolDelegate, DudelViewDelegate, MFMailComposeViewControllerDelegate> {
id <Tool> currentTool;
IBOutlet DudelView *dudelView;
IBOutlet UIBarButtonItem *textButton;
IBOutlet UIBarButtonItem *freehandButton;
IBOutlet UIBarButtonItem *ellipseButton;
IBOutlet UIBarButtonItem *rectangleButton;
IBOutlet UIBarButtonItem *lineButton;
IBOutlet UIBarButtonItem *dotButton;
UIColor *strokeColor;
UIColor *fillColor;
CGFloat strokeWidth;
UIFont *font;
}
@property (retain, nonatomic) id <Tool> currentTool;
@property (retain, nonatomic) UIColor *strokeColor;
@property (retain, nonatomic) UIColor *fillColor;
@property (assign, nonatomic) CGFloat strokeWidth;
@property (retain, nonatomic) UIFont *font;
- (IBAction)touchTextItem:(id)sender;
- (IBAction)touchFreehandItem:(id)sender;
- (IBAction)touchEllipseItem:(id)sender;
- (IBAction)touchRectangleItem:(id)sender;
- (IBAction)touchLineItem:(id)sender;
- (IBAction)touchDotItem:(id)sender;
- (IBAction)touchSendPdfEmailItem:(id)sender;
@end
2. Setting Up the GUI
We'll also need a pair
of images, normal and highlighted, for the Text tool button. Either grab
these from the book's code archive or make something on your own,
similar to what's shown in Table 1.
Table 1. New Buttons for the Text Tool
Filename | Image |
---|
button_text.png | |
button_text_selected.png | |
Add those to your project alongside the other button images, and then open DudelViewController.xib in Interface Builder. Make sure you can see toolbar at the bottom of the Dudel View window. Drag a new UIBarButtonItem
from the Library to the toolbar, placing it to the right of the other
tools, but to the left of the flexible space. Use the attribute
inspector to set its Image to button_text.png and set its Style to Plain. Figure 1 shows the end result.
Now control-drag from the new button to the File's Owner in the main .nib window, and select the touchTextItem: action from the menu that appears. Then control-drag from the File's Owner back to the button, and select the textButton outlet from the menu. That's all the GUI configuration we need to do, so you can save your work and go back to Xcode.